﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using System.Collections.Specialized;
using System.Data.SqlClient;

public partial class QLRaoVat : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        lblError.Visible = false;
    }
    protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
    {

    }

    protected void btTimKiem_Click(object sender, EventArgs e)
    {
        DataService ds = new DataService();
        ds.Connect();
        GridView1.DataBind();
        ds.Disconnect();
    }

    protected string HighlightText(string searchWord, string inputText)
    {

        Regex expression = new Regex(searchWord.Replace(" ", "|"), RegexOptions.IgnoreCase);

        return expression.Replace(inputText, new MatchEvaluator(ReplaceKeywords));

    }

    public string ReplaceKeywords(Match m)
    {

        return "<span style='background-color:#507CD1;color: white;font-weight: bold;'>" + m.Value + "</span>";

    }
    protected void btXoaChon_Click(object sender, EventArgs e)
    {
        //Create String Collection to store 
        //IDs of records to be deleted 
        StringCollection idCollection = new StringCollection();
        string strID = string.Empty;

        //Loop through GridView rows to find checked rows 
        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            CheckBox chkDelete = (CheckBox)GridView1.Rows[i].Cells[0].FindControl("chkItem");

            if (chkDelete != null)
            {
                if (chkDelete.Checked)
                {
                    strID = ((Label)GridView1.Rows[i].Cells[1].FindControl("Label3")).Text;
                    idCollection.Add(strID);
                }
            }
        }

        //Call the method to Delete records 
        DeleteMultipleRecords(idCollection);

        // rebind the GridView
        GridView1.DataBind();
    }

    private void DeleteMultipleRecords(StringCollection idCollection)
    {
        string IDs = "'";

        foreach (string id in idCollection)
        {
            IDs += id.ToString() + "','";
        }

        DataService ds = new DataService();
        ds.Connect();
        try
        {
            if (IDs.Length > 1)
            {
                string strIDs = IDs.Substring(0, IDs.LastIndexOf(","));
                string strSql = "Delete from Raovat WHERE Maraovat in (" + strIDs + ")";
                Label5.Text = strSql;
                ds.ExecuteNoneQuery(strSql);
            }
        }
        catch (SqlException exp)
        {
            //lblError.Text = "Lỗi: " + exp.Message;
            //lblError.BackColor = System.Drawing.Color.;
            //lblError.ForeColor = System.Drawing.Color.White;
            lblError.Visible = true;
        }
        //GridView1.DataBind();
        ds.Disconnect();
    }

    //this is for header checkbox changed event
    protected void chkHeader_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cbSelectedHeader = (CheckBox)GridView1.HeaderRow.FindControl("chkHeader");//if u checked header checkbox automatically all the check boxes will be checked,viseversa

        foreach (GridViewRow row in GridView1.Rows)
        {
            CheckBox cbSelected = (CheckBox)row.FindControl("chkItem");

            if (cbSelectedHeader.Checked == true)
                cbSelected.Checked = true;
            else
                cbSelected.Checked = false;
        }
    }
}